package com.manager.plat.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigBusinessDao;
import com.manager.plat.dao.IListWhiteDao;
import com.manager.plat.vo.ConfigBusinessVo;
import com.manager.plat.vo.ListWhiteVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ListWhiteAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8726321674366824496L;
	private static Logger logger = Logger.getLogger(ListWhiteAction.class);
	
	private IListWhiteDao listWhiteDao;			//白名单数据实现
	private IConfigBusinessDao configBusinessDao;//业务配置数据接口层
	private List<ListWhiteVo> listWhiteVoList;	//白名单数据列表
	private List<ConfigBusinessVo> configBusinessVoList;//业务配置信息列表
	private ListWhiteVo listWhiteVo;				//白名单信息VO
	private String actionPath;					//操作成功跳转路径
	
	public String  getListWhiteList(){
		listWhiteVoList = listWhiteDao.getListWhiteList();
		return SUCCESS;
	}
	
	public String addListWhite(){
		listWhiteVo = null;
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String editListWhite(){
		if(listWhiteVo.getId() != 0){
			listWhiteVo = listWhiteDao.getListWhiteVoById(listWhiteVo.getId());
		}
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String updateListWhite(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		if(listWhiteVo.getId() == 0){
			if("0".equals(listWhiteVo.getSp_code())){
				List<ConfigBusinessVo> configBusinessList = configBusinessDao.getConfigBusinessList();
				if(configBusinessList != null && configBusinessList.size() > 0){
					List<ListWhiteVo> listWhiteList = new ArrayList<ListWhiteVo>();
					String[] mobiles = listWhiteVo.getMobile().split(":");
					for(ConfigBusinessVo configBusinessVo : configBusinessList){
						if(mobiles != null && mobiles.length > 0){
							for(String mobile : mobiles){
								ListWhiteVo listWhite = new ListWhiteVo();
								listWhite.setMobile(mobile);
								listWhite.setSp_code(configBusinessVo.getSp_code());
								
								listWhiteList.add(listWhite);
							}
						}
					}
					if(listWhiteList != null && listWhiteList.size() > 0){
						int count = listWhiteDao.batchInsertListWhite(listWhiteList);
						
						if(count > 0){
							logger.info(String.format("[%s]添加[%s]白名单信息成功", platUser.getLogin_username(),listWhiteVo.getMobile()));
						}else{
							logger.info(String.format("[%s]添加[%s]白名单信息失败", platUser.getLogin_username(),listWhiteVo.getMobile()));
						}
					}
				}
			}else{
				String[] mobiles = listWhiteVo.getMobile().split(":");
				if(mobiles != null && mobiles.length > 0){
					List<ListWhiteVo> listWhiteList = new ArrayList<ListWhiteVo>();
					for(String mobile : mobiles){
						ListWhiteVo listWhite = new ListWhiteVo();
						listWhite.setMobile(mobile);
						listWhite.setSp_code(listWhiteVo.getSp_code());
						
						listWhiteList.add(listWhite);
					}
					
					if(listWhiteList != null && listWhiteList.size() > 0){
						int count = listWhiteDao.batchInsertListWhite(listWhiteList);
						
						if(count > 0){
							logger.info(String.format("[%s]添加[%s]白名单信息成功", platUser.getLogin_username(),listWhiteVo.getMobile()));
						}else{
							logger.info(String.format("[%s]添加[%s]白名单信息失败", platUser.getLogin_username(),listWhiteVo.getMobile()));
						}
					}
				}
			}
		}else {
			int count = listWhiteDao.updateListWhite(listWhiteVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]白名单信息成功", platUser.getLogin_username(),listWhiteVo.getMobile()));
			}else{
				logger.info(String.format("[%s]更新[%s]白名单信息失败", platUser.getLogin_username(),listWhiteVo.getMobile()));
			}
		}
		
		actionPath = "listWhite/getListWhiteList.action";
		return SUCCESS;
	}
	
	public String deleteListWhite(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		int count = listWhiteDao.deleteListWhite(listWhiteVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]白名单信息成功", platUser.getLogin_username(),listWhiteVo.getMobile()));
		}else{
			logger.info(String.format("[%s]删除[%s]白名单信息失败", platUser.getLogin_username(),listWhiteVo.getMobile()));
		}
		actionPath = "listWhite/getListWhiteList.action";
		return SUCCESS;
	}

	
	public IListWhiteDao getListWhiteDao() {
		return listWhiteDao;
	}
	public void setListWhiteDao(IListWhiteDao listWhiteDao) {
		this.listWhiteDao = listWhiteDao;
	}
	public IConfigBusinessDao getConfigBusinessDao() {
		return configBusinessDao;
	}
	public void setConfigBusinessDao(IConfigBusinessDao configBusinessDao) {
		this.configBusinessDao = configBusinessDao;
	}
	public List<ListWhiteVo> getListWhiteVoList() {
		return listWhiteVoList;
	}
	public void setListWhiteVoList(List<ListWhiteVo> listWhiteVoList) {
		this.listWhiteVoList = listWhiteVoList;
	}
	public List<ConfigBusinessVo> getConfigBusinessVoList() {
		return configBusinessVoList;
	}
	public void setConfigBusinessVoList(List<ConfigBusinessVo> configBusinessVoList) {
		this.configBusinessVoList = configBusinessVoList;
	}
	public ListWhiteVo getListWhiteVo() {
		return listWhiteVo;
	}
	public void setListWhiteVo(ListWhiteVo listWhiteVo) {
		this.listWhiteVo = listWhiteVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
